package com.crayon2f.test.thread;

import lombok.SneakyThrows;
import org.junit.Test;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * Created by feifan.gou@gmail.com on 2020/12/24 14:57.
 */
public class ThreadSecurity {


    @Test
    @SneakyThrows
    public void testHashMap() {

//        Map<Integer, String> map = new ConcurrentHashMap<>();
        Map<Integer, String> map = new HashMap<>();

        Thread thread = new Thread(() -> {
            for (int i = 0; i < 100000; i++) {
                map.put(i, Integer.toBinaryString(i));
            }
        }, "线程1");
        Thread thread2 = new Thread(() -> {
            for (int i = 0; i < 100000; i++) {
                map.put(i, Integer.toBinaryString(i));
            }
        },"线程2");

        thread.start();
        thread2.start();
        thread.join();
        thread2.join();

        System.out.println(map.size());
    }
}
